﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace Bmz.Infrastructure.Util
{
    public class PortUtil
    {
        public static bool IsUsedPort(int port)
        {
            IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
            foreach(IPEndPoint endpoint in ipEndPoints)
            {
                if(endpoint.Port==port)
                {
                    return true;
                }
            }
            return false;
        }

        public static int GetLeisurePort(int port)
        {
            while (true)
            {
                if (PortUtil.IsUsedPort(port))
                {
                    port++;
                }
                else
                {
                    return port;
                }
            }
        }
    }
}
